package com.tengju.bff.interfaces.covercharge.controller.admin;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.idanchuang.component.base.JsonResult;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeErrorCode;
import com.tengju.bff.interfaces.covercharge.entity.vo.*;
import com.tengju.bff.interfaces.covercharge.service.IServiceRewardPunishCategoryService;
import com.tengju.bff.interfaces.covercharge.util.validator.ValidaUtil;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.zoi7.component.core.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: xx
 * @Description: 违约金类目controller
 * @Date: 2020/12/14 20:09
 */
@Api(value = "punish/category", tags = "Admin基础信息页相关")
@RestController
@Slf4j
@RequestMapping("/manage/punish/category")
@ManageEntrance
public class AdminPunishCategoryController {

    @Resource
    private IServiceRewardPunishCategoryService serviceRewardPunishCategoryService;


    /**
     * 根据天健获取类目信息
     *
     * @param adminCategoryPageQuery 类目请求Query
     * @return 查询结果
     */
    @PostMapping("/getInfo")
    @ApiOperation(value = "基础信息页类目信息查询", notes = "基础信息页类目信息查询")
    public ApiResponse<IPage<AdminCategoryVo>> getCategoryInfo(@RequestBody AdminCategoryPageQuery adminCategoryPageQuery) {
        return ApiResponse.newSuccess(serviceRewardPunishCategoryService.getCategoryInfo(adminCategoryPageQuery));
    }

    @PostMapping("/updateInfo")
    @ApiOperation(value = "基础信息页类目信息更改", notes = "基础信息页类目信息更改")
    public ApiResponse<?> updateCategoryInfo(@RequestBody AdminCategoryUpdate adminCategoryUpdate) {
        ValidaUtil.baseReqParamCheck(adminCategoryUpdate);
        //获取更新实体
        List<AdminCategoryVo> updateList = adminCategoryUpdate.getCategoryList();
        //循环更改状态
        for (AdminCategoryVo adminCategoryVo : updateList) {
            //更改状态 启用/禁用
            try {
                serviceRewardPunishCategoryService.updateCategoryStatus(adminCategoryVo.getId(), adminCategoryUpdate.getStatus(), adminCategoryUpdate.getOperate());
            } catch (Exception e) {
                log.info("[支付后台][基础信息页][更新类目信息状态失败][类目id:{},需要更新的状态{}]", adminCategoryVo.getId(), adminCategoryUpdate.getStatus());
            }

        }
        return ApiResponse.newSuccess();
    }

    @PostMapping("/insertCategory")
    @ApiOperation(value = "基础信息页类目新增", notes = "基础信息页类目新增")
    public ApiResponse<?> insertCategory(@RequestBody AdminCategorySave adminCategorySave) {
        ValidaUtil.baseReqParamCheck(adminCategorySave);
        if (CollectionUtil.isEmpty(adminCategorySave.getCategoryList())) {
            log.info("[支付后台][基础信息页][新增类目失败，必填参数缺失]");
            ApiResponse.newFailure(CoverChargeErrorCode.PARAM_ERROR.getCode(), "必填参数缺失");
        }
        //获取新增list
        List<AdminCategoryVo> insertList = adminCategorySave.getCategoryList();
        for (AdminCategoryVo adminCategoryVo : insertList
        ) {
            if (StringUtils.isBlank(adminCategoryVo.getTitle())) {
                log.info("[支付后台][基础信息页][新增类目失败，类目值为空]");
                return ApiResponse.newFailure(CoverChargeErrorCode.PARAM_ERROR.getCode(), "必填参数类目值缺失");
            }
            try {
                serviceRewardPunishCategoryService.insertCategory(adminCategoryVo, adminCategorySave.getOperate());
            } catch (ServiceException e) {
                log.info("[支付后台][基础信息页][新增类目失败，该类目已存在][类目:{}]", adminCategoryVo.getTitle());
                return ApiResponse.newFailure(CoverChargeErrorCode.BUSINESS_FAIL.getCode(), "该类目已存在");
            }
        }
        return ApiResponse.newSuccess();
    }

    @PostMapping("/insertCategoryInfo")
    @ApiOperation(value = "基础信息页类目值新增", notes = "基础信息页类目值新增")
    public ApiResponse<?> insertCategoryInfo(@RequestBody AdminCategorySave adminCategorySave) {
        //获取新增list
        ValidaUtil.baseReqParamCheck(adminCategorySave);
        if (CollectionUtil.isEmpty(adminCategorySave.getCategoryList())) {
            log.info("[支付后台][基础信息页][新增类目值失败，必填参数缺失]");
            ApiResponse.newFailure(CoverChargeErrorCode.PARAM_ERROR.getCode(), "必填参数缺失");
        }
        //获取新增list
        List<AdminCategoryVo> insertList = adminCategorySave.getCategoryList();
        for (AdminCategoryVo adminCategoryVo : insertList) {
            if (adminCategoryVo.getParentId() == null || StringUtils.isBlank(adminCategoryVo.getTitle())) {
                log.info("[支付后台][基础信息页][新增类目值失败，必填参数缺失]");
                return ApiResponse.newFailure(CoverChargeErrorCode.PARAM_ERROR.getCode(), "必填参数缺失");
            }
            try {
                serviceRewardPunishCategoryService.insertCategoryValue(adminCategoryVo, adminCategorySave.getOperate());
            } catch (ServiceException e) {
                log.info("[支付后台][基础信息页][新增类目失败，该类目值已存在][类目值:{}]", adminCategoryVo.getTitle());
                return ApiResponse.newFailure(CoverChargeErrorCode.BUSINESS_FAIL.getCode(), "该类目值已存在");
            }
        }
        return ApiResponse.newSuccess();
    }

    @PostMapping("/getCategoryInfoList")
    @ApiOperation(value = "基础类目信息查询", notes = "基础类目信息查询")
    public ApiResponse<List<AdminCategoryVo>> getCategoryInfoList(@RequestBody AdminCategoryQuery adminCategoryQuery) {
        return ApiResponse.newSuccess(serviceRewardPunishCategoryService.getCategoryInfoList(adminCategoryQuery));
    }
}
